let app = getApp();
import { uploadImg } from "../../utils/request";
import { submitComplaintSuggest } from "../../api/complaints";
import { baseImgUrl } from "../../setting";
Page({
  data: {
    imgBaseUrl: baseImgUrl,
    selectTypeId: '',
    selectTypeName: '',
    suggestCate: '1',
    suggestTitle: '',
    suggestContent: '',
    typeTitle: [
      { title: '商品名称', id: '1' },
      { title: '商家名称', id: '2' },
      { title: '订单编号', id: '3' },
    ],
    curTilte: '0',
    curLength: 0,
    curPlaceholder: '商品名称',
    imgArr: []
  },
  onLoad(option) {
    this.setData({
      selectTypeId: option.typeId,
      selectTypeName: option.typeName
    })
  },
  onShow(){
    setTimeout(() => { my.hideLoading() }, 500);
  },
  howToObtain(){
    my.navigateTo({
      url: '../howToObtain/howToObtain'
    });
  },
  titleClick(e){
    const curId = e.target.dataset.id
    this.setData({
      curTilte: curId-1,
      suggestCate: curId,
      curPlaceholder: curId == 1 ? '商品名称'  :
      curId == 2 ? '商家名称'  : '订单编号'
    })
  },
  typeInput(e){
    // console.log('输入', e.detail.value, e.detail.value.length)
    this.setData({
      suggestTitle: e.detail.value,
      curLength: e.detail.value.length
    })
  },
  onContent(e) {
    this.setData({
      suggestContent: e.detail.value,
    })
  },
  uploadPhoto(){
    const that = this;
    let arr = []
    uploadImg(function (url) {
      if (url) {
        if(that.data.imgArr.indexOf(url) != -1){
          my.showToast({
            type: 'none',
            content: '当前图片已存在,请重新上传',
            duration: 1000
          });
          return
        }
        arr = [url]
        let arr2 = [...that.data.imgArr,...arr]
        that.setData({
          imgArr: arr2
        })
        // console.log('imgArr',that.data.imgArr)
      }
    });
  },
  // 查看选择图片 
  watchImg(e){
    let i = e.target.dataset.index;
    console.log('watchImg',i, this.data.imgArr)
    my.previewImage({
      current: i,
      urls: this.data.imgArr,
    }); 
  },
  // 删除图片
  deleteImg(e){
    let i = e.target.dataset.index; 
    console.log('deleteImg',i, this.data.imgArr)
    this.data.imgArr.splice(i,1);
    // this.data.complainImg.splice(i,1);
    this.setData({
      imgArr: this.data.imgArr,
      // complainImg:this.data.complainImg,
      // imgCount : 3 - this.data.imgArr.length
    })
  },
  submit(){
    const {selectTypeId, suggestCate, suggestTitle, suggestContent, imgArr} = this.data
    if(!suggestTitle){
      my.showToast({
        type: 'none',
        content: '请填写投诉的内容',
        duration: 1000
      });
      return
    }
    if(!suggestContent){
      my.showToast({
        type: 'none',
        content: '请输入详细情况说明',
        duration: 1000
      });
      return
    }
    if(!imgArr.length){
      my.showToast({
        type: 'none',
        content: '请上传凭证',
        duration: 1000
      });
      return
    }
    let obj = {
      suggest_type_id: selectTypeId,
      suggest_cate: suggestCate,
      suggest_title: suggestTitle,
      content: suggestContent,
      images: this.data.imgArr.join(','),
    }
    // console.log('提交', obj);
    submitComplaintSuggest(obj).then(res =>{
      my.showToast({
        type: 'success',
        content: '提交成功',
        duration: 1500
      });
      my.navigateTo({ url: '../complaintManage/complaintManage' });
    })
  }
});
